/*
    This file is part of jTopaz.
    http://gcl.cis.udel.edu/projects/jtopaz
    Copyright (C) 2009 University of Delaware

    jTopaz is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    jTopaz is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of                                                        
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with jTopaz.  If not, see <http://www.gnu.org/licenses/>.
*/
import org.globus.ftp.GridFTPClient;
import org.globus.myproxy.MyProxy;
import org.ietf.jgss.GSSCredential;
import java.io.File;
import jTopaz.*;

public class UploadTest {

	public static void main(String[] args) {
		
		try {
		GridFTPClient transferClient = new GridFTPClient("rocks-97.sdsc.edu", 2811);
		GridFTPClient infoClient = new GridFTPClient("rocks-97.sdsc.edu", 2811);
		MyProxy MPServer = new MyProxy("grid-devel1.rocksclusters.org", 7512);
		
		GSSCredential cred = MPServer.get("flujul@sdsc.edu", "bobnewhart", 1000);
		
		transferClient.authenticate(cred);
		infoClient.authenticate(cred);
		System.out.println("clients authenticated");
		
		
		//the file to upload
		File localFile = new File("/Users/patrickmcclory/Desktop/Firefox 2.0.0.6.dmg");
		
		//check if it already exists remotely, if so remove it
		
		
		if (infoClient.exists(localFile.getName())) {
			infoClient.deleteFile(localFile.getName());
		}
		
		//client.put(localFile, localFile.getName(), false);
		
		UploadThread upload = new UploadThread(transferClient, localFile, localFile.getName());
		
		
		RemoteFileMonitor monitor = new RemoteFileMonitor(localFile.getName(), localFile.length(), infoClient, cred);
		
		
		upload.start();
		
		monitor.run();
		
		while(! upload.transferComplete()) {
			//System.out.println("doing the transfer?");
			System.out.println("Speed: " + monitor.getSpeed());
			System.out.println("Progress: " + monitor.getProgress() + "\n\n");
		}
		
		System.out.println("transfer complete");
		
		}
		
		catch(Exception e) {
			System.out.println(e);
			e.printStackTrace();
		}
	}	
}